package com.hgm.flopgame2.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Button
import androidx.viewpager.widget.ViewPager
import com.hgm.flopgame2.adapter.IntroductionAdapter
import com.hgm.flopgame2.R
import com.hgm.flopgame2.databinding.ActivityStartBinding

class StartActivity : AppCompatActivity() {

    private val binding: ActivityStartBinding by lazy {
        ActivityStartBinding.inflate(layoutInflater)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        //隐藏状态栏
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        initView()
    }


    private fun initView() {
        //把页面添加到集合里
        var pagerList = mutableListOf<View>()
        pagerList.add(getView(R.layout.layout_start_one))
        pagerList.add(getView(R.layout.layout_start_two))
        pagerList.add(getView(R.layout.layout_start_three))

        //设置适配器
        binding.viewPager.adapter = IntroductionAdapter(pagerList)
        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> binding.apply {
                        pOne.setImageResource(R.drawable.bg_introduction_current)
                        pTwo.setImageResource(R.drawable.bg_introduction_normal)
                        pThree.setImageResource(R.drawable.bg_introduction_normal)
                    }
                    1 -> binding.apply {
                        pOne.setImageResource(R.drawable.bg_introduction_normal)
                        pTwo.setImageResource(R.drawable.bg_introduction_current)
                        pThree.setImageResource(R.drawable.bg_introduction_normal)
                    }
                    2 -> binding.apply {
                        pOne.setImageResource(R.drawable.bg_introduction_normal)
                        pTwo.setImageResource(R.drawable.bg_introduction_normal)
                        pThree.setImageResource(R.drawable.bg_introduction_current)
                    }
                }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
        binding.pOne.setImageResource(R.drawable.bg_introduction_current)//默认第一位

        //获取第三个页面的按钮
        pagerList[2].findViewById<Button>(R.id.btn_jump).setOnClickListener {
            startActivity(Intent(this, LoginActivity::class.java))
        }
    }


    /**
     * 初始化布局填充器把数组里的布局添加里面
     */
    private fun getView(res: Int): View {
        return LayoutInflater.from(this).inflate(res, null)
    }
}